package translated;

import java.util.List;
import java.util.ArrayList;
import edu.pku.sei.modeler.model.core.*;
import edu.pku.sei.modeler.model.structure.*;
import edu.pku.sei.modeler.model.notation.core.*;
import edu.pku.sei.modeler.model.notation.shape.*;
import edu.pku.sei.modeler.model.notation.mapping.*;
import edu.pku.sei.modeler.jet.util.*;

public class NodeEditPartGen
{
  protected static String nl;
  public static synchronized NodeEditPartGen create(String lineSeparator)
  {
    nl = lineSeparator;
    NodeEditPartGen result = new NodeEditPartGen();
    nl = null;
    return result;
  }

  public final String NL = nl == null ? (System.getProperties().getProperty("line.separator")) : nl;
  protected final String TEXT_1 = "/**" + NL + " * Copyright (c) Software Institute of PKU" + NL + " * All rights reserved. " + NL + " * This program and the accompanying materials" + NL + " * are made available under the terms of the Eclipse Public License v1.0" + NL + " * which accompanies this distribution, and is available at" + NL + " * http://www.eclipse.org/legal/epl-v10.html" + NL + " *" + NL + " */" + NL + "package ";
  protected final String TEXT_2 = ";" + NL + "" + NL + "import java.beans.PropertyChangeEvent;" + NL + "import java.util.List;" + NL + "import java.util.ArrayList;" + NL + "" + NL + "import org.eclipse.draw2d.Figure;" + NL + "import org.eclipse.draw2d.IFigure;" + NL + "import org.eclipse.draw2d.Label;" + NL + "import org.eclipse.draw2d.geometry.Rectangle;" + NL + "import org.eclipse.gef.EditPolicy;" + NL + "import org.eclipse.gef.commands.Command;" + NL + "import org.eclipse.gef.commands.UnexecutableCommand;" + NL + "import org.eclipse.gef.editpolicies.DirectEditPolicy;" + NL + "import org.eclipse.gef.editpolicies.GraphicalNodeEditPolicy;" + NL + "import org.eclipse.gef.requests.CreateConnectionRequest;" + NL + "import org.eclipse.gef.requests.ReconnectRequest;" + NL + "import org.eclipse.swt.graphics.Color;" + NL + "" + NL + "import edu.pku.notation.runtime.draw2d.TextFigure;" + NL + "import edu.pku.sei.modeler.edit.parts.AbstractNodeEditPart;" + NL + "import edu.pku.sei.modeler.edit.policies.StructuredLayoutEditPolicy;" + NL + "import edu.pku.sei.modeler.commands.CreateLinkCommand;" + NL + "import edu.pku.sei.modeler.images.ImageProvider;" + NL + "import edu.pku.sei.modeler.model.core.MetaElement;" + NL + "import edu.pku.sei.modeler.model.structure.MetaDiagram;" + NL + "import edu.pku.sei.modeler.model.structure.MetaLink;" + NL + "import edu.pku.sei.modeler.model.structure.MetaNode;" + NL + "import edu.pku.sei.modeler.model.structure.MetaShapeElement;" + NL + "import edu.pku.sei.modeler.model.structure.MetaLocationManager;" + NL + "" + NL + "import ";
  protected final String TEXT_3 = ".";
  protected final String TEXT_4 = ";" + NL + "import ";
  protected final String TEXT_5 = ".";
  protected final String TEXT_6 = "Figure;";
  protected final String TEXT_7 = NL + "import ";
  protected final String TEXT_8 = ".";
  protected final String TEXT_9 = ";";
  protected final String TEXT_10 = NL;
  protected final String TEXT_11 = NL + "import ";
  protected final String TEXT_12 = ".";
  protected final String TEXT_13 = ";";
  protected final String TEXT_14 = NL + NL + "/**" + NL + " * @generated" + NL + " *" + NL + " */" + NL + "public class ";
  protected final String TEXT_15 = "Editpart extends AbstractNodeEditPart " + NL + "{" + NL + "\tpublic ";
  protected final String TEXT_16 = "Editpart (MetaShapeElement model) {" + NL + "\t\tsuper(model);" + NL + "\t}" + NL + "\tpublic void activate() {" + NL + "\t\tsuper.activate();" + NL + "\t\t";
  protected final String TEXT_17 = " model = (";
  protected final String TEXT_18 = ")((MetaNode)getModel()).getModelElement();" + NL + "\t\tmodel.addPropertyChangeListener(modelListener);\t" + NL + "\t\tList childrenList = new ArrayList();" + NL + "\t\t";
  protected final String TEXT_19 = NL + "\t\tchildrenList.add(model.get";
  protected final String TEXT_20 = "());" + NL + "\t\t";
  protected final String TEXT_21 = NL + "\t\tMetaNode node = getNode();" + NL + "\t\tMetaDiagram diagram = node.getDiagram();" + NL + "\t\tList nodes = diagram.getNodes();" + NL + "\t\tfor(int i=0; i<nodes.size(); i++) {" + NL + "\t\t\tMetaNode tmp = (MetaNode)nodes.get(i);" + NL + "\t\t\tfor(int j=0; j<childrenList.size(); j++) {" + NL + "\t\t\t\tList children = (List)childrenList.get(j);" + NL + "\t\t\t\tif(children.contains(tmp.getModelElement())) {" + NL + "\t\t\t\t\tnode.addToManager(tmp);" + NL + "\t\t\t\t}" + NL + "\t\t\t}" + NL + "\t\t}" + NL + "\t\t";
  protected final String TEXT_22 = "\t" + NL + "\t}" + NL + "" + NL + "\tpublic void deactivate() {" + NL + "\t\t";
  protected final String TEXT_23 = " model = (";
  protected final String TEXT_24 = ")((MetaNode)getModel()).getModelElement();" + NL + "\t\tmodel.removePropertyChangeListener(modelListener);" + NL + "\t\tsuper.deactivate();" + NL + "\t}" + NL + "\t/* (non-Javadoc)" + NL + "\t * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()" + NL + "\t */" + NL + "\tprotected IFigure createFigure() {" + NL + "\t\tLabel header = new Label(\"GeneratedShape\");" + NL + "\t\t";
  protected final String TEXT_25 = "Figure figure = new ";
  protected final String TEXT_26 = "Figure();" + NL + "\t\tfigure.add(header);" + NL + "\t\treturn figure;" + NL + "\t}" + NL + "" + NL + "\tprotected DirectEditPolicy createDirectEditPolicy() {" + NL + "\t\treturn null;" + NL + "\t}" + NL + "\t" + NL + "\t@Override" + NL + "\tprotected IFigure getDirectEditFigure() {" + NL + "\t\tLabel header = (Label)getFigure();" + NL + "\t\treturn header;" + NL + "\t}" + NL + "" + NL + "\t/* (non-Javadoc)" + NL + "\t * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()" + NL + "\t */" + NL + "\tprotected void createEditPolicies() {" + NL + "\t\tsuper.createEditPolicies();" + NL + "\t\t";
  protected final String TEXT_27 = NL + "\t\tinstallEditPolicy(EditPolicy.LAYOUT_ROLE, new StructuredLayoutEditPolicy());" + NL + "\t\t";
  protected final String TEXT_28 = NL + "\t\tinstallEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE, new GraphicalNodeEditPolicy() {" + NL + "\t\t\tprotected Command getConnectionCompleteCommand(CreateConnectionRequest request) {" + NL + "\t\t\t\t";
  protected final String TEXT_29 = NL + "\t\t\t\treturn UnexecutableCommand.INSTANCE;" + NL + "\t\t\t\t";
  protected final String TEXT_30 = NL + "\t\t\t\tMetaLink newObj = (MetaLink)request.getNewObject();" + NL + "\t\t\t\tMetaElement ele = newObj.getModelElement();" + NL + "\t\t\t\t";
  protected final String TEXT_31 = NL + "\t\t\t\tif(ele instanceof ";
  protected final String TEXT_32 = ") {" + NL + "\t\t\t\t\tCreateLinkCommand cmd = (CreateLinkCommand)request.getStartCommand();" + NL + "\t\t\t\t\tcmd.setTarget((MetaNode)getHost().getModel());" + NL + "\t\t\t\t\treturn cmd;" + NL + "\t\t\t\t}" + NL + "\t\t\t\t\t\t\t";
  protected final String TEXT_33 = NL + "\t\t\t\treturn null;" + NL + "\t\t\t}" + NL + "\t\t\tprotected Command getConnectionCreateCommand(CreateConnectionRequest request) {" + NL + "\t\t\t\t";
  protected final String TEXT_34 = NL + "\t\t\t\treturn UnexecutableCommand.INSTANCE;" + NL + "\t\t\t\t";
  protected final String TEXT_35 = NL + "\t\t\t\tMetaLink newObj = (MetaLink)request.getNewObject();" + NL + "\t\t\t\tMetaElement ele = newObj.getModelElement();" + NL + "\t\t\t\t";
  protected final String TEXT_36 = NL + "\t\t\t\tif(ele instanceof ";
  protected final String TEXT_37 = ") {" + NL + "\t\t\t\t\tCommand cmd = new CreateLinkCommand(newObj," + NL + "\t\t\t\t\t(MetaNode)getHost().getModel());" + NL + "\t\t\t\t\trequest.setStartCommand(cmd);" + NL + "\t\t\t\t\treturn cmd;" + NL + "\t\t\t\t}" + NL + "\t\t\t\t\t\t\t";
  protected final String TEXT_38 = NL + "\t\t\t\treturn null;" + NL + "\t\t\t}" + NL + "\t\t\tprotected Command getReconnectSourceCommand(ReconnectRequest request) {" + NL + "\t\t\t\treturn UnexecutableCommand.INSTANCE;" + NL + "\t\t\t}" + NL + "\t\t\tprotected Command getReconnectTargetCommand(ReconnectRequest request) {" + NL + "\t\t\t\treturn UnexecutableCommand.INSTANCE;" + NL + "\t\t\t}" + NL + "\t\t});" + NL + "\t}" + NL + "" + NL + "\tprotected void handlePropertyChanged(PropertyChangeEvent event) {" + NL + "\t\tString propertyName = event.getPropertyName();" + NL + "\t\tsuper.handlePropertyChanged(event);" + NL + "\t}" + NL + "\t" + NL + "\tprivate ";
  protected final String TEXT_39 = " get";
  protected final String TEXT_40 = "() {" + NL + "\t\treturn (";
  protected final String TEXT_41 = ")((MetaNode)getModel()).getModelElement();" + NL + "\t}" + NL + "\t" + NL + "\tprotected void refreshVisuals() {" + NL + "\t\tsuper.refreshVisuals();" + NL + "\t\t";
  protected final String TEXT_42 = " modelEle = (";
  protected final String TEXT_43 = ")getNode().getModelElement();" + NL + "\t\t";
  protected final String TEXT_44 = "Figure figure = (";
  protected final String TEXT_45 = "Figure)getFigure();" + NL + "\t\t";
  protected final String TEXT_46 = NL + "\t\t{" + NL + "\t\t\tString text";
  protected final String TEXT_47 = " = \"\";" + NL + "\t\t\t";
  protected final String TEXT_48 = NL + "\t\t\tTextFigure tFigure";
  protected final String TEXT_49 = " = figure.getText";
  protected final String TEXT_50 = "();" + NL + "\t\t\ttFigure";
  protected final String TEXT_51 = ".setText(text";
  protected final String TEXT_52 = ");" + NL + "\t\t}" + NL + "\t\t";
  protected final String TEXT_53 = NL + "\t\t" + NL + "\t\t";
  protected final String TEXT_54 = NL + "\t\tMetaNode node = getNode();" + NL + "\t\tint x = node.getLocationX();" + NL + "\t\tint y = node.getLocationY();" + NL + "\t\tint width = node.getWidth();" + NL + "\t\tint height = node.getHeight();" + NL + "\t\tRectangle r = new Rectangle(x, y, width, height);" + NL + "\t\tList mgrs = node.getLocationMgrs();" + NL + "\t\tfor(int i=0; i<mgrs.size(); i++) {" + NL + "\t\t\tMetaLocationManager mgr = (MetaLocationManager)mgrs.get(i);" + NL + "\t\t\tmgr.locateSubParts(r);" + NL + "\t\t}" + NL + "\t\t";
  protected final String TEXT_55 = NL + "\t}" + NL + "}";
  protected final String TEXT_56 = NL;

  public String generate(Object argument)
  {
    final StringBuffer stringBuffer = new StringBuffer();
     MetaClass cls = (MetaClass) argument; 
    stringBuffer.append(TEXT_1);
    stringBuffer.append(ModelerConstants.pkgBase + "." + ModelerConstants.editpartBase);
    stringBuffer.append(TEXT_2);
    stringBuffer.append(PackageRetriever.retrievePackage(cls));
    stringBuffer.append(TEXT_3);
    stringBuffer.append(cls.getName());
    stringBuffer.append(TEXT_4);
    stringBuffer.append(ModelerConstants.pkgBase + "." + ModelerConstants.figureBase);
    stringBuffer.append(TEXT_5);
    stringBuffer.append(cls.getName());
    stringBuffer.append(TEXT_6);
    
	// import relations
	MetaModel model = cls.getModel();
	List relations = null;
	if(model != null) {
		relations = model.getInstancesForType(CorePackage.__METARELATIONSHIP__);
	}

    
	for(int i=0; i<relations.size(); i++) {
		MetaRelationship relation = (MetaRelationship)relations.get(i);

    stringBuffer.append(TEXT_7);
    stringBuffer.append(PackageRetriever.retrievePackage(relation));
    stringBuffer.append(TEXT_8);
    stringBuffer.append(relation.getName());
    stringBuffer.append(TEXT_9);
    
	}

    stringBuffer.append(TEXT_10);
    
	// import references
	List refs = cls.getReferences();
	for(int i=0; i<refs.size(); i++) {
		MetaReference ref = (MetaReference)refs.get(i);
		MetaClass refCls = (MetaClass)ref.getType();

    stringBuffer.append(TEXT_11);
    stringBuffer.append(PackageRetriever.retrievePackage(refCls));
    stringBuffer.append(TEXT_12);
    stringBuffer.append(refCls.getName());
    stringBuffer.append(TEXT_13);
    
	}

    stringBuffer.append(TEXT_14);
    stringBuffer.append(cls.getName());
    stringBuffer.append(TEXT_15);
    stringBuffer.append(cls.getName());
    stringBuffer.append(TEXT_16);
    stringBuffer.append(cls.getName());
    stringBuffer.append(TEXT_17);
    stringBuffer.append(cls.getName());
    stringBuffer.append(TEXT_18);
    
			List mgrs = cls.getModel().getLocationManagers(cls.getElementID());
			if(mgrs.size() > 0) {
				for(int i=0; i<mgrs.size(); i++) {
					MetaLocationManager mgr = (MetaLocationManager)mgrs.get(i);
		
    stringBuffer.append(TEXT_19);
    stringBuffer.append(mgr.getRefName());
    stringBuffer.append(TEXT_20);
    
				}
		
    stringBuffer.append(TEXT_21);
    
			}
		
    stringBuffer.append(TEXT_22);
    stringBuffer.append(cls.getName());
    stringBuffer.append(TEXT_23);
    stringBuffer.append(cls.getName());
    stringBuffer.append(TEXT_24);
    stringBuffer.append(cls.getName());
    stringBuffer.append(TEXT_25);
    stringBuffer.append(cls.getName());
    stringBuffer.append(TEXT_26);
    
			if(cls.getModel().getLocationManagers(cls.getElementID()).size() > 0) {
		
    stringBuffer.append(TEXT_27);
    
			}
		
    stringBuffer.append(TEXT_28);
    
					if(relations == null) {
				
    stringBuffer.append(TEXT_29);
    
					}
				
    stringBuffer.append(TEXT_30);
    
					for(int i=0; i<relations.size(); i++) {
						MetaRelationship relation = (MetaRelationship)relations.get(i);
						if(cls.isTypeOf(relation.getTargetType())) {
							
    stringBuffer.append(TEXT_31);
    stringBuffer.append(relation.getName());
    stringBuffer.append(TEXT_32);
    
						}
					}
				
    stringBuffer.append(TEXT_33);
    
					if(relations == null) {
				
    stringBuffer.append(TEXT_34);
    
					}
				
    stringBuffer.append(TEXT_35);
    
					for(int i=0; i<relations.size(); i++) {
						MetaRelationship relation = (MetaRelationship)relations.get(i);
						if(cls.isTypeOf(relation.getSourceType())) {
							
    stringBuffer.append(TEXT_36);
    stringBuffer.append(relation.getName());
    stringBuffer.append(TEXT_37);
    
						}
					}
				
    stringBuffer.append(TEXT_38);
    stringBuffer.append(cls.getName());
    stringBuffer.append(TEXT_39);
    stringBuffer.append(cls.getName());
    stringBuffer.append(TEXT_40);
    stringBuffer.append(cls.getName());
    stringBuffer.append(TEXT_41);
    stringBuffer.append(cls.getName());
    stringBuffer.append(TEXT_42);
    stringBuffer.append(cls.getName());
    stringBuffer.append(TEXT_43);
    stringBuffer.append(cls.getName());
    stringBuffer.append(TEXT_44);
    stringBuffer.append(cls.getName());
    stringBuffer.append(TEXT_45);
    
			// for Property-binding
			Notation notation = null;
			CoreMetaModel cmm = (CoreMetaModel)cls.getModel();
			List nlist = cmm.getNotations();
			for (int i = 0; i < nlist.size();i++) {
				Notation n = (Notation)nlist.get(i);
				if (n.getModelID() == cls.getElementID()) {
					notation = n;
					break;
				}//end if
			}//end for
			
			// get all TextObject so that we can generate getters for them
			// and then we can call the getters to get them, and change their text
			// used for Property-binding
			List<TextObject> textObjs = new ArrayList<TextObject>();
			if(notation != null) {
				textObjs = notation.getAllTextObj();
			}
			for(int i=0; i<textObjs.size(); i++) {
				TextObject tobj = textObjs.get(i);
				if(tobj.getTextMapping() == null)continue;
				MappingChain chain = tobj.getTextMapping().getChain();
				if(chain instanceof TextMappingChain) {
					TextMappingChain tmc = (TextMappingChain)chain;
		
    stringBuffer.append(TEXT_46);
    stringBuffer.append(tobj.getElementID());
    stringBuffer.append(TEXT_47);
    stringBuffer.append(MappingHelper.genMappingCode(tmc, "modelEle", "text"+tobj.getElementID(), 0));
    stringBuffer.append(TEXT_48);
    stringBuffer.append(tobj.getElementID());
    stringBuffer.append(TEXT_49);
    stringBuffer.append(tobj.getElementID());
    stringBuffer.append(TEXT_50);
    stringBuffer.append(tobj.getElementID());
    stringBuffer.append(TEXT_51);
    stringBuffer.append(tobj.getElementID());
    stringBuffer.append(TEXT_52);
    
				}
			}
		
    stringBuffer.append(TEXT_53);
    
			// for generating location managers
			if(cls.getModel().getLocationManagers(cls.getElementID()).size() > 0) {
		
    stringBuffer.append(TEXT_54);
    
			}
		
    stringBuffer.append(TEXT_55);
    stringBuffer.append(TEXT_56);
    return stringBuffer.toString();
  }
}
